package com.lamda.zhekou.manager.impl;


import com.lamda.zhekou.dao.GenericDao;
import com.lamda.zhekou.dao.UserDao;
import com.lamda.zhekou.exception.ZheKouException;
import com.lamda.zhekou.manager.PageModel;
import com.lamda.zhekou.manager.UserManager;
import com.lamda.zhekou.model.User;
import com.lamda.zhekou.vo.UserVpsSelectVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by MJiao on 15/8/13.
 */
@Service
public class UserManagerImpl implements UserManager {

    @Autowired
    private UserDao userDao;

    /**
     * 注册
     *
     * @param user
     * @throws ZheKouException
     */
    @Override
    public void register(User user) throws ZheKouException {
        User tUser = this.userDao.findByParameterAndSingleResult("email", user.getEmail(), GenericDao.QueryParameterUtil.EQUAL);
        if (tUser != null) {
            throw new ZheKouException("邮箱已被注册");
        }
//        User tUser2 = this.userDao.findByParameterAndSingleResult("inviteCode", user.getFilledInviteCode(), GenericDao.QueryParameterUtil.EQUAL);
//        if (tUser2 == null) {
//            throw new ZheKouException("邀请码不存在");
//        }
        user.setRegisterTime(new Timestamp(System.currentTimeMillis()));
        this.userDao.persist(user);
    }

    /**
     * 登陆
     *
     * @param email
     * @param password
     */
    @Override
    public User login(String email, String password) throws ZheKouException {
        User tUser = this.userDao.findByParameterAndSingleResult("email", email, GenericDao.QueryParameterUtil.EQUAL);
        if (tUser == null) {
            throw new ZheKouException("该邮箱还未注册");
        }
        if (!tUser.getPassword().equals(password)) {
            throw new ZheKouException("密码不正确");
        }
        return tUser;
    }

    /**
     * 分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param asc      是否为升序查询
     * @return
     */
    @Override
    public PageModel<User> paging(int pageNo, int pageSize, boolean asc) {
        return this.userDao.paging(pageNo, pageSize, asc);
    }

    @Override
    public List<UserVpsSelectVo> getUsersByFyEmailFilter(String keyWord) {
        List<User> userList = this.userDao.getUsersByFyEmailFilter(keyWord);
        List<UserVpsSelectVo> userVpsSelectVoList = new ArrayList<>();
        for (User user : userList) {
            UserVpsSelectVo userVpsSelectVo = new UserVpsSelectVo();
            userVpsSelectVo.setEmail(user.getEmail());
            userVpsSelectVo.setNickname(user.getNickname());
            userVpsSelectVoList.add(userVpsSelectVo);
        }
        return userVpsSelectVoList;
    }

    @Override
    public User getUsersByFyEmail(String userEmail) {
        return this.userDao.findByParameterAndSingleResult("email", userEmail, GenericDao.QueryParameterUtil.EQUAL);
    }

    @Override
    public User findUserById(Long userId) {
        return this.userDao.findById(userId);
    }

    @Override
    public List<User> getAllAdmins() {
        return this.userDao.findAllAdmins();
    }

    @Override
    public void cancelUserAdmin(Long userId) throws ZheKouException {
        User user = this.findUserById(userId);
        if (user == null) {
            throw new ZheKouException("您要操作的用户不存在");
        }
        user.setRole(0);
        this.userDao.update(user);
    }

    @Override
    public void setUserToAdmin(Long userId) throws ZheKouException {
        User user = this.findUserById(userId);
        if (user == null) {
            throw new ZheKouException("您要操作的用户不存在");
        }
        /**
         * 如果邀请码为空则生成邀请码
         */
        if (StringUtils.isEmpty(user.getInviteCode())){
            user.setInviteCode(Long.toHexString(userId + 10000));
        }
        user.setRole(1);
        this.userDao.update(user);
    }

    @Override
    public void modifyPassword(Long userId, String oldPasswrd, String password, String confirmPassword) throws ZheKouException {
        User user = this.userDao.findById(userId);
        if (!user.getPassword().equals(oldPasswrd)) {
            throw new ZheKouException("原始密码错误");
        }
        if (!password.equals(confirmPassword)) {
            throw new ZheKouException("密码不匹配");
        }
        user.setPassword(password);
        this.userDao.update(user);
    }

    @Override
    public List<User> findAllCommonUser() {
        return this.userDao.findAllCommonUser();
    }
}
